package main

import (
	"flag"
	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	"github.com/gin-gonic/gin"
	"go-admin-official/modules/auth"
	"go-admin-official/modules/config"
	"go-admin-official/modules/database"
	"go-admin-official/modules/logger"
	"go-admin-official/modules/pay"
)

func main() {

	var c string

	flag.StringVar(&c, "c", "./config.yaml", "config path")
	flag.Parse()

	// 初始化配置
	if err := config.InitFromYaml(c); err != nil {
		panic(err)
	}

	// 初始化日志
	logger.InitWithConfig(config.Get().Logger, config.Get().Debug)

	// 初始化数据库
	database.InitMYSQL()

	// 初始化session
	auth.InitJWT()

	// 初始化gin设置

	if config.Get().Debug {
		gin.SetMode(gin.DebugMode)
	}

	pay.InitAliPay()
	pay.InitWxPay()

	r := gin.New()

	store := cookie.NewStore([]byte(config.Get().AppSecret))
	r.Use(sessions.Sessions("goadmin_official_session", store))

	r.Static("/assets", config.Get().Frontend.AssetsPath)
	r.LoadHTMLGlob(config.Get().Frontend.PagePath)

	// 构建路由
	buildRouters(r)
	// 启动服务器
	startServer(r)
}
